package userService

import (
	"chart.zozoo.net/app/dao/usersDao"
	"chart.zozoo.net/app/models"
	"chart.zozoo.net/core/myRedis"
	"errors"
	"github.com/go-redis/redis"
	"time"
)

func InfoByUuid(uuid string) (u *models.Users, err error) {
	//根据uuid获取用户真实id
	uuidKey := "users:uuid:" + uuid + ":int"
	var id int
	id, err = myRedis.RedisClient.Get(uuidKey).Int()

	if err != nil && !errors.Is(redis.Nil, err) {
		return nil, err
	}

	u = &models.Users{}
	if id == 0 {
		//缓存不存在，直接查询数据库
		id, err = usersDao.GetId(uuid)
		if err != nil {
			return nil, err
		}

		//保存缓存
		myRedis.RedisClient.Set(uuidKey, id, time.Hour*24)
	}

	//通过id查询用户信息
	return Info(id)
}
